/*
 * 版权所有 (C) 2001-2014 深圳市艾派应用系统有限公司。保留所有权利。
 * 版本：
 * 修改记录：
 *		1、2014-11-21，tfl创建。 
 */
package com.chinamobile.ln.ictenterprises.utils;

import java.util.HashMap;
import java.util.Map;
import org.json.JSONException;
import android.util.Log;
import com.chinamobile.ln.ictenterprises.R;
import com.chinamobile.ln.ictenterprises.common.GlobalSetting;
import com.chinamobile.ln.ictenterprises.common.MainApplication;
import com.chinamobile.ln.ictenterprises.model.response.ResponseGetContact;
import com.chinamobile.ln.ictenterprises.model.response.ResponseHistory;
import com.chinamobile.ln.ictenterprises.model.response.ResponseHistoryDetail;
import com.chinamobile.ln.ictenterprises.model.response.ResponseLogin;
import com.chinamobile.ln.ictenterprises.model.response.ResponseLogout;
import com.chinamobile.ln.ictenterprises.model.response.ResponseSendMsg;
import com.chinamobile.ln.ictenterprises.model.response.ResponseVersion;

/**
 * JSON数据解析工具类
 **/
public class JsonParse
{
    private static final String TAG = "JsonParse";
    public static String no_data = MainApplication.getInstance().getString(
        R.string.no_data);

    /**
     * json数据解析
     * @param _httpResponseData
     * @param _jsonParseType
     * @return
     * @throws JSONException
     */
    public static Map<String, Object> parseJSON(String _httpResponseData,
        int _jsonParseType)
    {
        HashMap<String, Object> dataMap = new HashMap<String, Object>();
        try
        {
            switch (_jsonParseType)
            {
            case GlobalSetting.USER_LOGIN:
                parseUserLogin(_httpResponseData, dataMap);
                break;
            case GlobalSetting.USER_LOGOUT:
                parseUserLogout(_httpResponseData, dataMap);
                break;
            case GlobalSetting.GET_CONTACT:
                praseGetContact(_httpResponseData, dataMap);
                break;
            case GlobalSetting.SEND_MSG:
                praseSendMsg(_httpResponseData, dataMap);
                break;
            case GlobalSetting.GET_HISTORY:
                praseHistory(_httpResponseData, dataMap);
                break;
            case GlobalSetting.UPDATE_VERSION:
                praseVersionUpdate(_httpResponseData, dataMap);
                break;
            case GlobalSetting.HISTORY_DETAIL:
                praseHistoryDetail(_httpResponseData, dataMap);
                break;
            default:
                break;
            }
        }
        catch (JSONException e)
        {
            Log.e(TAG, "解析JSON异常");
        }
        return dataMap;
    }

    /**
     * 解析用户登录
     * @param dataStr
     * @param dataMap
     * @throws JSONException
     */
    private static void parseUserLogin(String dataStr,
        Map<String, Object> dataMap) throws JSONException
    {
        ResponseLogin responseLogin = (ResponseLogin) JsonUtil.jsonToBean(
            dataStr, ResponseLogin.class);
        dataMap.put(GlobalSetting.JSON_USER_LOGIN, responseLogin);
    }

    /**
     * 解析用户登出
     * @param dataStr
     * @param dataMap
     * @throws JSONException
     */
    private static void parseUserLogout(String dataStr,
        Map<String, Object> dataMap) throws JSONException
    {
        ResponseLogout responseLogout = (ResponseLogout) JsonUtil.jsonToBean(
            dataStr, ResponseLogout.class);
        dataMap.put(GlobalSetting.JSON_USER_LOGOUT, responseLogout);
    }

    /**
     * 解析获取联系人
     * @param dataStr
     * @param dataMap
     * @throws JSONException
     */
    private static void praseGetContact(String dataStr,
        Map<String, Object> dataMap) throws JSONException
    {
        ResponseGetContact responseGetContact = (ResponseGetContact) JsonUtil
            .jsonToBean(dataStr, ResponseGetContact.class);
        dataMap.put(GlobalSetting.JSON_GET_CONTACT, responseGetContact);
    }

    /**
     * 解析发送短信
     * @param dataStr
     * @param dataMap
     * @throws JSONException
     */
    private static void praseSendMsg(String dataStr, Map<String, Object> dataMap)
        throws JSONException
    {
        ResponseSendMsg responseSendMsg = (ResponseSendMsg) JsonUtil
            .jsonToBean(dataStr, ResponseSendMsg.class);
        dataMap.put(GlobalSetting.JSON_SEND_MSG, responseSendMsg);
    }

    /**
     * 解析历史记录
     * @param dataStr
     * @param dataMap
     * @throws JSONException
     */
    private static void praseHistory(String dataStr, Map<String, Object> dataMap)
        throws JSONException
    {
        ResponseHistory responseHistory = (ResponseHistory) JsonUtil
            .jsonToBean(dataStr, ResponseHistory.class);
        dataMap.put(GlobalSetting.JSON_HISTORY, responseHistory);
    }

    /**
     * 解析版本更新
     * @param dataStr
     * @param dataMap
     * @throws JSONException
     */
    private static void praseVersionUpdate(String dataStr,
        Map<String, Object> dataMap) throws JSONException
    {
        ResponseVersion responseVersion = (ResponseVersion) JsonUtil
            .jsonToBean(dataStr, ResponseVersion.class);
        dataMap.put(GlobalSetting.JSON_VERSION_UPDATE, responseVersion);
    }

    /**
     * 解析获取历史详情
     * @param dataStr
     * @param dataMap
     * @throws JSONException
     */
    private static void praseHistoryDetail(String dataStr,
        Map<String, Object> dataMap) throws JSONException
    {
        ResponseHistoryDetail responseHistoryDetail = (ResponseHistoryDetail) JsonUtil
            .jsonToBean(dataStr, ResponseHistoryDetail.class);
        dataMap.put(GlobalSetting.JSON_HISTORY_DETAIL, responseHistoryDetail);
    }

}
